$(document).ready(function(){
	
	var blankValue = "输入待审查药物";
	//1、AutoComplete
	//为每个输入框增加提示和自动完成
	var textHintAndAutoComplete = function(option){
		return function(){
			var _this = this;
			//输入提示
			$(this).texthint({blankValue:blankValue,blankValueClass:'hui-color',hasValueClass:'hei-color'});
			//AutoComplete
			var opt_q = {
		      	serviceUrl: URL_DRUG_SUGGEST,
			  	minChars:option.minChars || 2,
		      	width: option.width,
			  	maxHeight:option.maxHeight || 115,
		      	deferRequestBy: 0,
		      	noCache: option.noCache || false,
		      	onSelect: function(value, data){
					$(_this).attr('drugId', data);
		      	},
		      	onFocus: function(value, data){
		      		$(_this).attr('drugId', data);		      		
		      	},
		      	onReset: function(){
		      		$(_this).removeAttr('drugId');
		      	}
			};

			$(this).autocomplete(opt_q);
		}
	}
	
	//审查首页自动完成
	$('.drug-one').each(textHintAndAutoComplete({width:530}));
	$('.drug-two').each(textHintAndAutoComplete({width:350}));
	
	//审查结果页自动完成
	$('.head-drug-one').each(textHintAndAutoComplete({width:394}));
	$('.head-drug-two').each(textHintAndAutoComplete({width:280}));
	

	//用药审查Tab切换
	$('.radios > ul > li > a').click(function(event){
		event.preventDefault();
		$('#drug_check_tab > form').hide();
		
		var formId = '#' + $(this).attr('name') + '_Form';
		$(formId).show();
	});
	
	//药物名称改变，删除原来的药物ID
	$('.review-k1').change(function(){
//		$(this).removeAttr('drugId');
	})
	
	//相互作用审查	
	$('#checkEffect_Btn').click(function(){
		var drugOne = {
			'id': '#drug_effect_check_input_one',
			'drugId': $('#drug_effect_check_input_one').attr('drugId'),
			'drugName': $('#drug_effect_check_input_one').val()
		};
		var drugTwo = {
			'id': '#drug_effect_check_input_two',
			'drugId': $('#drug_effect_check_input_two').attr('drugId'),
			'drugName': $('#drug_effect_check_input_two').val()
		}
		
		//非空检查
		if(drugOne.drugName == blankValue || drugTwo.drugName == blankValue){
			alert('请输入待审查药物。');
			return false;
		}
		
		var submitForm = function(){
			$('#effect_drug_ids').val(drugOne.drugId + ',' + drugTwo.drugId);
			addDrugCheckCookie('checkEffect', drugOne, drugTwo);
			$('#checkEffect_Form').submit();
		}
		
		var drugOneCallBack, drugTwoCallBack;
		if(!drugTwo.drugId && drugTwo.drugName != blankValue){
			drugTwoCallBack = autoGetDrugIdByName(drugTwo, submitForm);
		}else{
			drugTwoCallBack = submitForm;
		}
		if(!drugOne.drugId && drugOne.drugName != blankValue){
			drugOneCallBack = autoGetDrugIdByName(drugOne,drugTwoCallBack);
		}else{
			drugOneCallBack = drugTwoCallBack;
		}
		
		drugOneCallBack();
		
	})
	
	//配伍禁忌审查
	$('#checkPatibility_Btn').click(function(){
		var drugOne = {
			'id': '#drug_pati_check_input_one',
			'drugId': $('#drug_pati_check_input_one').attr('drugId'),
			'drugName': $('#drug_pati_check_input_one').val()
		};
		var drugTwo = {
			'id': '#drug_pati_check_input_two',
			'drugId': $('#drug_pati_check_input_two').attr('drugId'),
			'drugName': $('#drug_pati_check_input_two').val()
		}
		
		var submitForm = function(){
			$('#effect_pati_ids').val(drugOne.drugId + ',' + drugTwo.drugId);
			addDrugCheckCookie('checkPatibility', drugOne, drugTwo);
			$('#checkPatibility_Form').submit();
		}
		
		var drugOneCallBack, drugTwoCallBack;
		if(!drugTwo.drugId && drugTwo.drugVal != blankValue){
			drugTwoCallBack = autoGetDrugIdByName(drugTwo, submitForm);
		}else{
			drugTwoCallBack = submitForm;
		}
		if(!drugOne.drugId && drugOne.drugVal != blankValue){
			drugOneCallBack = autoGetDrugIdByName(drugOne,drugTwoCallBack);
		}else{
			drugOneCallBack = drugTwoCallBack;
		}
		
		drugOneCallBack();
	})
	
	//禁慎用
	//Bug fixed:Form仅包含一个Input时，在Input中输入回车会触发表单的提交
	$('#drug_disease_check_input').keydown(function(event) {
		if (event.keyCode == '13') {
			event.preventDefault();
			$('#checkCare_Btn').trigger('click');
		}
	});
	
	$('#checkCare_Btn').click(function(){
		var drug = {
			'id': '#drug_disease_check_input',
			'drugId': $('#drug_disease_check_input').attr('drugId'),
			'drugName': $('#drug_disease_check_input').val()
		}
		
		var submitForm = function(){
			$('#care_drug_ids').val(drug.drugId);
			addDrugCheckCookie('checkCare', drug);
			$('#checkCare_Form').submit();
		}
		
		var drugCallBack;
		if(!drug.drugId && drug.drugVal != blankValue){
			drugCallBack = autoGetDrugIdByName(drug,submitForm);
		}else{
			drugCallBack = submitForm;
		}
		
		drugCallBack();
	})
	
	var addDrugCheckCookie = function(checkType, drugOne, drugTwo){
		var drugNames, drugId;
		if(checkType == 'checkCare'){
			drugNames = drugOne.drugName,
			drugId = drugOne.drugId;
		}else{
			drugNames = drugOne.drugName + '+' + drugTwo.drugName,
			drugId = drugOne.drugId + '+' + drugTwo.drugId;
		}
		var cookie = checkType +'@' + drugNames + '@' + drugId;
		$.addData2Cookie('drug_check_settings','check',cookie);
	}
	
	var autoGetDrugIdByName = function(drug, callback){
		return function(){
			$.post('http://drugs.medlive.cn/res/js/drugref/drug_full_name_search_ajax.do', {q: drug.drugName}, function(res){
				if(res.success){
					$(drug.id).attr('drugId', res.id);
					drug.drugId = res.id;
					if(callback){
						callback();
					}
				}else{
					alert('您输入的药品名称:' + drug.drugName + '暂不存在！');
				}				
			},'json')			
		}
	}
	
	//For 用药审查头部通用Form
	if(typeof checkType != 'undefined' && checkType != ''){
		$('.radios > ul > li > a[name="' + checkType + '"]').trigger('click');
	}

});